using System;
using System.Collections;
using System.Collections.Specialized;

namespace IECAS.ERS.Services.Pop3
{
  /// <summary>
  /// Object representation of an email message.
  /// </summary>
    public class Pop3EmailMessage
    {
        #region Private variables

        private Hashtable headers;
        private string from, to, cc, subject;
        private string body;

        #endregion

        /// <summary>
        /// Constructs a new EmailMessage object.
        /// </summary>
        public Pop3EmailMessage()
        {
            headers = CollectionsUtil.CreateCaseInsensitiveHashtable();
        }

        #region Access properties/methods

        /// <summary>
        /// Returns the value of the given header.
        /// </summary>
        /// <param name="name">The name of the header to get the value for.</param>
        /// <returns>The value of the header as a string array; if the header exists 
        /// several times, the array contains multiple strings.</returns>
        public string[] GetHeader(string name)
        {
            return headers[name] as string[];
        }

        /// <summary>
        /// Sets the value of the given header.
        /// </summary>
        /// <param name="name">The name of the header.</param>
        /// <param name="value">The new value set for the header</param>
        public void SetHeader(string name, string[] value)
        {
            headers[name] = value;
        }

        /// <summary>
        /// Adds a new header value.
        /// </summary>
        /// <param name="name">The name of the header to which the value shall be added.</param>
        /// <param name="value">The value string to be added.</param>
        public void AddHeaderValue(string name, string value)
        {
            string[] values = GetHeader(name);
            if (values == null)
                values = new string[1];
            else
            {
                string[] newV = new string[values.Length + 1];
                values.CopyTo(newV, 0);
                values = newV;
            }
            values[values.Length - 1] = value;
            SetHeader(name, values);
        }

        /// <summary>
        /// Returns all available header names for this message.
        /// </summary>
        /// <returns></returns>
        public ICollection GetHeaderNames()
        {
            return headers.Keys;
        }

        /// <summary>
        /// The sender of the message.
        /// </summary>
        public string From
        {
            get { return from; }
            set { from = value; }
        }

        /// <summary>
        /// The recipient(s) of the message.
        /// </summary>
        public string To
        {
            get { return to; }
            set { to = value; }
        }

        /// <summary>
        /// The CC list of the message; in practice, comma separated email 
        /// addresses with possible realnames embedded.
        /// </summary>
        public string CC
        {
            get { return cc; }
            set { cc = value; }
        }

        /// <summary>
        /// The subject line of the message.
        /// </summary>
        public string Subject
        {
            get { return subject; }
            set { subject = value; }
        }

        public string Body
        {
            get { return body; }
            set { body = value; }
        }

        #endregion

        private string references;
        public string References
        {
            get { return references; }
            set { references = value; }
        }
        private string inReplyTo;
        public string InReplyTo
        {
            get { return inReplyTo; }
            set { inReplyTo = value; }
        }

        private string messageID;
        public string MessageID
        {
            get { return messageID; }
            set { messageID = value; }
        }

        private string contentID;
        public string ContentID
        {
            get { return contentID; }
            set { contentID = value; }
        }

        private string boundary;
        public string Boundary
        {
            get { return boundary; }
            set { boundary = value; }
        }

        private string date;
        public string Date
        {
            get { return date; }
            set { date = value; }
        }

        private string uidl;
        public string UIDL
        {
            get { return uidl; }
            set { uidl = value; }
        }
        public string EncodingName;

        public System.Collections.Generic.List<IECAS.ERS.Services.Pop3.Pop3Message.Pop3MessageContent> ContentList { get; set; }

        public System.Collections.Generic.List<IECAS.ERS.Services.Pop3.Pop3Message.Pop3MessageAttachment> AttachmentList { get; set; }

        public void AddContentList(IECAS.ERS.Services.Pop3.Pop3Message.Pop3MessageContent content)
        {
            if (ContentList == null)
                ContentList = new System.Collections.Generic.List<IECAS.ERS.Services.Pop3.Pop3Message.Pop3MessageContent>();
            ContentList.Add(content);
        }

        public void AddAttachmentList(IECAS.ERS.Services.Pop3.Pop3Message.Pop3MessageAttachment att)
        {
            if (AttachmentList == null)
                AttachmentList = new System.Collections.Generic.List<IECAS.ERS.Services.Pop3.Pop3Message.Pop3MessageAttachment>();
            AttachmentList.Add(att);
        }

        public string ContentTransferEncoding { get; set; }
    }

}